import {defineStore} from 'pinia'
import xe from "xe-utils";
import config from "@/config";
import router from "@/router";
export const useGlobal = defineStore('globalStore', {
	state: () => {
		return {
			units: [],
			goodCats:[],
			cart: {
				billId: null,
				buId: 0,
				buName: '',
				cartIndex: '',
			},
			refreshRouterViewKey:'',
			contactCatList: [],
			statusBarHeight: 29,
			//
			skList: [],
			ismobile: false,
			toBfList: [], // 我的拜访 页面的数据列表
			bfTabActive: 1, // 拜访页面的上面tab
			bfSubTabActive: '已完成',
			bfSets: {
				taskNo: 0, // 总任务数量
				rate: 0, // 完成率
				doneNo: 0, // 完成数量
				per: '',
				id: null,
				perNo: 1,
				useType: ''
			}, // 拜访设置
			bf: {}, // 拜访页面内容
			targetWidth: 800, // 照片宽度
			targetHeight: 800, // 照片宽度
			quality: 70, // 照片质量
			freshLoad: {
				currentPage: 1,
				refreshing: false, // 下拉刷新
				upLoading: false, // 上拉加载状态
				finished: false, // 上拉加载完成 显示没有更多了=true
			},
			deliver: {
				item: {},
				title: '',
				page: ''
			},
			socket: null,
			formOptions: {
				submitBtn: false,
				resetBtn: false,
				form: {
					//行内表单模式
					inline: false,
					//表单域标签的位置，如果值为 left 或者 right 时，则需要设置 label-width
					labelPosition: "right",
					//表单域标签的后缀
					labelSuffix: undefined,
					//是否显示必填字段的标签旁边的红色星号
					hideRequiredAsterisk: false,
					//表单域标签的宽度，例如 '50px'。作为 Form 直接子元素的 form-item 会继承该值。支持 auto。
					labelWidth: "80px",
					//是否显示校验错误信息
					showMessage: true,
					//是否以行内形式展示校验信息
					inlineMessage: false,
					//是否在输入框中显示校验结果反馈图标
					statusIcon: false,
					//是否在 rules 属性改变后立即触发一次验证
					validateOnRuleChange: true,
					//是否禁用该表单内的所有组件。若设置为 true，则表单内组件上的 disabled 属性不再生效
					disabled: false,
					//用于控制该表单内组件的尺寸 default / small / large
					size: 'default',
				},
			},
			// v3
			width: 0,
			height: 0,
			dialogOpened: false,
			vgridHeaderHeight: 0, // 表格上面搜索栏动态高度
			scanString: '', // 扫描枪
			areaList: [],
			pageSizes: [50, 500, 1000],
			pageSize: 50,
			pageSizeMobile: 20,
			routeList: [],
			viewTags: [],
			keepLiveRoute: [],
			routeKey: null,
			routeShow: true,
			//布局
			layout: config.LAYOUT,
			//菜单是否折叠 toggle
			menuIsCollapse: config.MENU_IS_COLLAPSE,
			//多标签栏
			layoutTags: config.LAYOUT_TAGS,
			//主题
			theme: config.THEME,
			currentRoute: {

			},
			iframeList: [],
			//
			editor: {
				id: '', // 编辑器ID
				value: '', // 编辑器值
				borderColor: '#ff9900', // 边框色
				color: 'dodgerblue',// 输入框字体色
				col: {}, // 编辑器字段参数
				params: {transType: 0, invId: 0, buId: 0}, // 编辑上传后台参数
			},
			eventAdd: 0, // 全局监听变化
			dept: [],
			menuList: [], // 菜单列表
			authBtns: [], // 页面按钮权限
			closeTab: false,
			contactList: [],
			providerCat: [], // 供应商分类
			goodList: [],
			wrapperList: [], // 包装物列表
			goodUnit: [], // 单位列表
			storageList: [],
			roleList: [], // 角色列表
			sendType: [], // 赠品类型
			accountList: [],
			adminList: [],
			outType: [],
			inType: [],
			goodCat: [],
			payWayList: [], // 结算方式
			incomeTypeList: [], // 收类别
			outcomeTypeList: [], // 支出类别
			customerCat: [],// 客户分类
			// 结算账户类别
			accountType: [],
			saleTransType: [],
			// 销售利润表 汇总类型
			profitGroup: [],
			// 采购汇总
			purGroupBy: [],
			refresh: {
				id: '', // 要刷新 的页面ID
				count: 0
			},
			menuStore: [],
			jsonMenuList: [], // 有权限的菜单
			sexList: []
		}
	},
	actions: {
		addCart (val) {

		},
		setBfTabActive (val) {
			this.bfTabActive = val
		},
		setDialogOpen (val) {
			this.dialogOpened = val
		},
		setIsMobile(key){
			this.ismobile = key
		},
		setLayout (key) {
			this.layout = key
		},
		toggleMenuIsCollapse () {
			this.menuIsCollapse = !this.menuIsCollapse
		},
		toggleLayoutTags () {
			this.layoutTags = !this.layoutTags
		},
		setIframeList(route){
			this.iframeList = []
			this.iframeList.push(route)
		},
		pushIframeList(route){
			let target = this.iframeList.find((item) => item.path === route.path)
			if(!target){
				this.iframeList.push(route)
			}
		},
		removeIframeList(route){
			this.iframeList.forEach((item, index) => {
				if (item.path === route.path){
					this.iframeList.splice(index, 1)
				}
			})
		},
		refreshIframe(route){
			this.iframeList.forEach((item) => {
				if (item.path == route.path){
					let url = route.meta.url;
					item.meta.url = '';
					setTimeout(function() {
						item.meta.url = url
					}, 200);
				}
			})
		},
		clearIframeList(){
			this.iframeList = []
		},
		pushKeepLive(component){
			if(!this.keepLiveRoute.includes(component)){
				this.keepLiveRoute.push(component)
			}
		},
		removeKeepLive(component){
			var index = this.keepLiveRoute.indexOf(component);
			if(index !== -1){
				this.keepLiveRoute.splice(index, 1);
			}
		},
		clearKeepLive(){
			this.keepLiveRoute = []
		},
		setRouteKey(key){
			this.routeKey = key
		},
		setRouteShow(key){
			this.routeShow = key
		},
		pushViewTags(route){
			let backPathIndex = this.viewTags.findIndex(item => item.fullPath == router.options.history.state.back)
			let target = this.viewTags.find((item) => item.fullPath === route.fullPath)
			let isName = route.name
			if(!target && isName){
				if(backPathIndex == -1){
					this.viewTags.push(route)
				}else{
					this.viewTags.splice(backPathIndex+1, 0, route)
				}
			}
		},
		removeViewTags(route){
			this.viewTags.forEach((item, index) => {
				if (item.fullPath === route.fullPath){
					this.viewTags.splice(index, 1)
				}
			})
		},
		updateViewTags(route){
			this.viewTags.forEach((item) => {
				if (item.fullPath == route.fullPath){
					item = Object.assign(item, route)
				}
			})
		},
		updateViewTagsTitle(title=''){
			const nowFullPath = location.hash.substring(1)
			this.viewTags.forEach((item) => {
				if (item.fullPath == nowFullPath){
					item.meta.title = title
				}
			})
		},
		clearViewTags(){
			this.viewTags = []
		},

		addRoute (route) {
			let current = xe.clone(route, true)
			if(route.routeName !== 'home') {
				let has = -1
				for (let i = 0; i < this.routeList.length; i++) {
					if(this.routeList[i].id === route.id) {
						has = i
					}
				}
				if(has === -1) {
					this.routeList.push(current)
				} else {
					this.routeList[has] = current
				}
				this.currentRoute = current
			}
			else {
				this.currentRoute = current
			}
		},

	}
})
